package com.tk.demo.controller;

import com.tk.demo.model.Student;
import com.tk.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Student> getStudents() {
        return studentService.findStudentList();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Student getStudentById(@PathVariable("id") int id) {
        return studentService.findOne(id);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String updateStudent(@PathVariable("id") int id, @RequestParam(value = "name", required = true) String name,
                                @RequestParam(value = "age", required = true) Integer age,
                                @RequestParam(value = "email", required = true) String email) {
        Student student = new Student();
        student.setName(name);
        student.setName(name);
        student.setId(id);
        Student student1 = studentService.saveAndFlush(student);

        return student1.toString();

    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public String postStudent(@RequestParam(value = "name") String name,
                              @RequestParam(value = "age") Integer age,
                              @RequestParam(value = "email") String email) {
        Student student = new Student();
        student.setAge(age);
        student.setName(name);
        student.setEmail(email);
        Student student1 = studentService.save(student);
        return student1.toString();

    }


}
